package com.songshuai.webapi.baseapi.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具
 *
 * @Author: lhx
 * @Date: 2018/11/1 14:16
 * @Version 1.0
 */
@Slf4j
public class DateUtil {

    public static String dateFormat = "yyyy-MM-dd";
    public static String dateFormat1 = "yyyy-MM-dd HH:mm:ss";
    public static String dateFormat2 = "yyyyMMddHHmmss";
    public static String dateFormat3 = "yyyy-MM-dd HH:mm";
    public static String dateFormat4 = "yyyyMMdd";

    public static Calendar calendar1900 = new GregorianCalendar(1900, 0, -1);
    public static Date date1990 = calendar1900.getTime();

    /**
     * 日期转字符串
     *
     * @param input
     * @return
     */
    public static String ChangeDateToString(Date input, String format) {
        String retData = "";
        if (input == null) {
            return retData;
        }
        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(input);
    }

    /**
     * 字符串转日期
     *
     * @param input
     * @return
     */
    public static Date ChangeStringToDate(String input, String format) {
        if (StringUtils.isBlank(input)) {
            return null;
        }

        if (StringUtils.isBlank(format)) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        try {
            return simpleDateFormat.parse(input);
        } catch (Exception e) {
            log.error("DateUtil.ChangeStringToDate()", e);
        }
        return null;
    }

    public static boolean isDateStr(String dateStr) {
        try {
            SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
            Date dd = fmt.parse(dateStr);
            if (dateStr.equals(fmt.format(dd))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            log.error("DateUtil.isDateStr()", e);
            return false;
        }
    }

    public static boolean isDateStr(String dateStr, String dateFormat) {
        try {
            SimpleDateFormat fmt = new SimpleDateFormat(dateFormat);
            Date dd = fmt.parse(dateStr);
            if (dateStr.equals(fmt.format(dd))) {
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            //log.error("DateUtil.isDateStr()",e);
            return false;
        }
    }

    public static Date dateFormat(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String dateStr = sdf.format(date);
        return ChangeStringToDate(dateStr, format);
    }

    /**
     * 根据时间推前多少年
     *
     * @param date
     * @param years
     * @return yyyy-MM-dd
     */
    public static Date rolBeforeYear(Date date, int years) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - years);
        return dateFormat(calendar.getTime(), dateFormat);
    }

    /**
     * 根据时间推后多少天
     *
     * @param date
     * @param days
     * @return yyyy-MM-dd
     */
    public static Date rolAfterDay(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + days);
        return dateFormat(calendar.getTime(), dateFormat);
    }

    /**
     * 计算时间距离当前时间多少秒
     * @param startDate
     * @param endDate
     * @return
     */
    public static int calcSecond(Date startDate, Date endDate){

        long a = startDate.getTime();
        long b = endDate.getTime();

        if(a >= b){
            return 0;
        }

        int c = (int)((b - a) / 1000);
        return c;
    }


    /**
     * 获取当前时间距离一天结束的剩余秒数
     * @param currentDate
     * @return
     */
    public static Long getRemainSecondsOneDay(Date currentDate) {
        LocalDateTime midnight = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault()).plusDays(1).withHour(0).withMinute(0)
                .withSecond(0).withNano(0);
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(currentDate.toInstant(),
                ZoneId.systemDefault());
        return ChronoUnit.SECONDS.between(currentDateTime, midnight);
    }


}
